package Eclass;

import ibatis.IbatisHelper;

import java.sql.SQLException;
import java.util.*;

import Eclass.ClassModel;

import com.ibatis.sqlmap.client.SqlMapClient;

public class ExamPrepManager {
	private SqlMapClient ibatis;

	public ExamPrepManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}

	
	//****************** EXAM PREP ********************
	public  List getRawExamPrepWithSearching(EclassForm eclass){
		List result = null;
		
		try{
			Map map = new HashMap();
			System.out.println("eclass.getParam_id() : "+eclass.getParam_idExamPrep());
			System.out.println("eclass.getParam_batchName() : "+eclass.getParam_batchName());
			System.out.println("eclass.getParam_className() : "+eclass.getParam_className());
			
			map.put("param_id", eclass.getParam_idExamPrep());
			map.put("param_batchName", eclass.getParam_batchName());
			map.put("param_className", eclass.getParam_className());
			result = ibatis.queryForList("eClass.getExamPrep", map);
			//System.out.println("***************RESULT****************\n"+result);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	/*public ExamPrepModel getListExamPrep(ExamPrepModel bean){
		return getBatchNClassIdByBatchClassId(getExamPrepByExamPrepId(bean.getExamPrepId()));
	}
	*/
	
	public Integer getNumOfExamPrepDetail(Integer examPrepId){
		Integer result = null;
		
		try{
			result = (Integer) ibatis.queryForObject("eClass.getNumOfExamPrepDetailByExamPrepId", examPrepId);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public Integer getCurExamPrepId(){
		Integer result = null;
		
		try{
			result = (Integer) ibatis.queryForObject("eClass.getCurExamPrepId", "");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public Integer getCurExamPrepDetailId(){
		Integer result = null;
		
		try{
			result = (Integer) ibatis.queryForObject("eClass.getCurExamPrepDetailId", "");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public  ExamPrepModel getExamPrepByExamPrepId(Integer idExamPrep){
		ExamPrepModel result = null;
		
		try{
			//System.out.println("idExamPrep : "+idExamPrep);
			result = (ExamPrepModel)ibatis.queryForObject("eClass.getExamPrepByExamPrepId", idExamPrep);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public  Integer getNumOfExPrepDetCandByExamPrepId(Integer examPrepId){
		Integer result = null;
		
		try{
			//System.out.println("idExamPrep : "+examPrepId);
			result = (Integer)ibatis.queryForObject("eClass.getNumOfExPrepDetCandByExamPrepId", examPrepId);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	
	public Boolean insertExamPrep(ExamPrepModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			System.out.println("*** insertExamPrep Bean: ");
			System.out.println("-----------------------------");
			this.ibatis.insert("eClass.insertExamPrep", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean deleteExamPrep(Integer examPrepId){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.delete("eClass.deleteExamPrep", examPrepId);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean cancelExamPrep(Integer examPrepId){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.delete("eClass.cancelExamPrep", examPrepId);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean cancelExamScore(Integer batchClassId){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.delete("eClass.cancelExamScore", batchClassId);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean updateExamPrep(ExamPrepModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.update("eClass.updateExamPrep", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean updateExPrepDetNumOfQuestForCandidate(ExamPrepDetailModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.update("eClass.updateExPrepDetNumOfQuestForCandidate", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	} 
	
	public Boolean updateStatusExamPrep(ExamPrepModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.update("eClass.updateStatusExamPrep", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	
	
	//*************************MS CLASS***************************
	public List getListClassFrmQuestPool(String examFor){
		List result = null;
		
		try{
			result = ibatis.queryForList("eClass.getListClassFrmQuestPool", examFor);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public List getUniqLstClassTraineeFrmQuestPool(Integer examPrepId){
		List result = null;
		
		try{
			result = ibatis.queryForList("eClass.getUniqLstClassTraineeFrmQuestPoolByExPrepId", examPrepId);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public List getUniqLstClassTraineeFrmQuestPoolWithAdd(Integer examPrepId, Integer classIdAdd){
		List result = null;
		
		try{
			Map inpt = new HashMap();
			inpt.put("examPrepId", examPrepId);
			inpt.put("classId", classIdAdd);
			result = ibatis.queryForList("eClass.getUniqLstClassTraineeFrmQuestPoolByExPrepIdWthAdd", inpt);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public ClassModel getListClassFrmQuestPoolById(Integer idClass){
		ClassModel result = null;
		
		try{
			result = (ClassModel)ibatis.queryForObject("eClass.getListClassFrmQuestPoolById", idClass);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public Integer getNumOfQuestForCandidate(){
		Integer result = null;
		
		try{
			result = (Integer)ibatis.queryForObject("eClass.getNumOfQuestForCandidate", "");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	
	/*public List getExamClassList(){
		List inpt = getListClass();
		List list = new ArrayList();
		int jml = inpt.size();
		for(int i=0;i<jml;i++){
			ClassModel cm = (ClassModel)inpt.get(i);
			String s = (cm.getClassName()).toUpperCase();
			if(s.contains("MID-TERM") || s.contains("COMPRE")){
				list.add(cm);
			}
		}
		return list; 
	}*/
	
	/*public List getClassWithoutExamList(){
		List inpt = getListClassFrmQuestPool();
		List list = new ArrayList();
		int jml = inpt.size();
		for(int i=0;i<jml;i++){
			ClassModel cm = (ClassModel)inpt.get(i);
			String s = (cm.getClassName()).toUpperCase();
			if(!s.contains("MID-TERM") && !s.contains("COMPRE")){
				list.add(cm);
			}
		}
		return list; 
	}*/
	
	//**************************END MS_CLASS**************************
	
	
	//****************** EXAM PREP DETAIL ********************
	
	
	
	public  List getExamPrepDetByExamPrepId(Integer examPrepId){
		List result = null;
		
		try{
			System.out.println("getExamPrepDetByExamPrepId : "+examPrepId);
			result = ibatis.queryForList("eClass.getExamPrepDetailByExamPrepId", examPrepId);
			System.out.println("getExamPrepDetByExamPrepId = "+ examPrepId);
			System.out.println(result.size()+"\n");
		}
		catch(Exception ex){
			ex.printStackTrace();
			result = null;
		}
		
		return result;
	}
	
	public ExamPrepDetailModel getExamPrepDetByExamPrepDetId(Integer examPrepDetId){
		ExamPrepDetailModel result = null;
		
		try{
			result = (ExamPrepDetailModel)ibatis.queryForObject("eClass.getExamPrepDetailByExamPrepDetailId", examPrepDetId);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	
	public Boolean insertExamPrepDetail(ExamPrepDetailModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			System.out.println("*** insertExamPrepDetail Bean: ");
			System.out.println("-----------------------------");
			bean.toString();
			this.ibatis.insert("eClass.insertExamPrepDetail", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean deleteExamPrepDetByExamPrepDetId(Integer examPrepDetId){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.delete("eClass.deleteExamPrepDetByExamPrepDetId", examPrepDetId);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean deleteExamPrepDetByExamPrepId(Integer idExamPrepId){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.delete("eClass.deleteExamPrepDetByExamPrepId", idExamPrepId);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean updateExamPrepDetail(ExamPrepDetailModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.update("eClass.updateExamPrepDetail", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean updateExamPrepDetailByIdExmPrepDet(ExamPrepDetailModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.update("eClass.updateExamPrepDetailByIdExmPrepDet", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	

}
